package com.sumainfor.zuul.controller;

import com.qiniu.util.Auth;
import com.sumainfor.common.NiuYunToken;
import com.sumainfor.common.util.JsonResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: zhlu
 * @Date: 2018/11/7 18:24
 * @Description:
 */
@Controller
public class NiuYunController {

    @ResponseBody
    @RequestMapping(value = {"/sys/NiuYun/Token", "wx/NiuYun/Token"})
    public JsonResult saveNiuYunToken() throws Exception {
        JsonResult resiult = new JsonResult();
        Map<String, Object> qiniu = new HashMap<String, Object>();
        try {
            Auth auth = Auth.create(NiuYunToken.QINIU_ACCESS_KEY, NiuYunToken.QINIU_SECRET_Key);
            String upToken = auth.uploadToken(NiuYunToken.publicBucketname);
            qiniu.put("token", upToken);
            qiniu.put("url", NiuYunToken.publicQiniuUrl);
            resiult.put(qiniu);
        } catch (Exception e) {
            System.out.println(e);
            resiult.putFailed("服务器繁忙,请稍后重试！");
        }
        return resiult;

    }
}
